<?php
/**
 * @package qBase
 * @subpackage web
 */

/**
 * QWebControlSelector
 * 
 * @package qBase
 * @subpackage web
 */
class QWebControlSelector extends QWebControl 
{
	/**
	 * The default class to be loaded 
	 *
	 * @var string
	 */
	protected $default_control_class = null;
	/**
	 * The default name o the control to be loaded 
	 *
	 * @var string
	 */
	protected $default_control_name = null;
	/**
	 * The default ID for the class to be loaded 
	 *
	 * @var string
	 */
	protected $default_control_id = null;
	/**
	 * The default class to be loaded 
	 *
	 * @var string
	 */
	protected $select_control_class = null;
	/**
	 * The default name o the control to be loaded 
	 *
	 * @var string
	 */
	protected $select_control_name = null;
	/**
	 * The default ID for the class to be loaded 
	 *
	 * @var string
	 */
	protected $select_control_id = null;
	
	protected $select_control_has_changed = false;
	
	protected $selected_control_loaded = false;
	
	public function __construct($id = null, $default_control_name = null, $default_control_class = null, $default_control_id = null)
	{
		parent::__construct($id);
		
		if ($default_control_name)
		{
			$this->default_control_name = $default_control_name;
			$this->default_control_class = $default_control_class;
			$this->default_control_id = $default_control_id;
		}

		$this->addDelegate("changeSelectedControl", new QWebDelegate($this, "___changeSelectedControl"));
	}
	
	public function getSelectEventLink($control_name = null, $control_class = null, $control_id = null)
	{
		if ($control_class && $control_id)
			return $this->getEventLink("changeSelectedControl", array("control_id" => $control_id, 
				"control_name" => $control_name, "control_class" => $control_class));
		else if ($control_class)
			return $this->getEventLink("changeSelectedControl", array("control_class" => $control_class, 
				"control_name" => $control_name));
		else 
			return $this->getEventLink("changeSelectedControl", array("control_name" => $control_name));
	}

	public function beforeInit()
	{
		parent::beforeInit();

		if ($this->selected_control_loaded)
			return ;
		
		$wp = $this->getWebPage();
		if (!$wp)
			throw new Exception("QWebControlSelector must be linked to a page before it's init");
			
		$forced_reload = false;
		$event = $wp->getTriggerEvent($this->getFullId(), "changeSelectedControl");
		$vs_peak = $wp->getViewstateData($this);

		if ($event)
		{
			// we have an event, use this information
			if (isset($event["params"]["control_id"])) $this->select_control_id = $event["params"]["control_id"];
			if (isset($event["params"]["control_name"])) $this->select_control_name = $event["params"]["control_name"];
			if (isset($event["params"]["control_class"])) $this->select_control_class = $event["params"]["control_class"];
			
			if (isset($event["params"]["forced_reload"])) $forced_reload = $event["params"]["forced_reload"];
		}
		else if ($vs_peak)
		{
			if (isset($vs_peak["control_id"])) $this->select_control_id    = $vs_peak["control_id"];
			if (isset($vs_peak["control_name"])) $this->select_control_name  = $vs_peak["control_name"];
			if (isset($vs_peak["control_class"])) $this->select_control_class = $vs_peak["control_class"];
		}
				
		// load something if we have any data : event > viewstate > default
		if ((!$this->select_control_name) && ($this->default_control_name))
		{
			// set the defaults
			$this->select_control_class = $this->default_control_class;
			$this->select_control_name  = $this->default_control_name;
			$this->select_control_id    = $this->default_control_id;
		}

		// now load if we have them
		if ($this->select_control_name)
		{
			if (!$this->select_control_class)
				$this->select_control_class = $this->select_control_name;
			$name = $this->select_control_name;
			$class = $this->select_control_class;
			$id = $this->select_control_id;
			if (!$id) $id = $name;
	
			if (QObject::IsA($class, "QWebControl"))
				eval("\$this->{$name} = new {$class}(\$id);");
			else 
				throw new Exception("Invalid panel class: {$class}");
				
			$this->removeAllControls();
			$this->addControl($this->{$name});

			$vs_class = null;
			$vs_name = null;
			if ($vs_peak)
			{
				if (isset($vs_peak["control_name"])) $vs_name = $vs_peak["control_name"];
				if (isset($vs_peak["control_class"])) $vs_class = $vs_peak["control_class"];
			}

			if ((!$forced_reload) && (!$this->select_control_has_changed) && 
						$vs_class && ($vs_class == $class) && ($vs_name == $name))
			{
				// no change
			}
			else 
			{
				// $this->debug("marked as changed" . uniqid());
				$this->select_control_has_changed = true;
				// $this->{$name}->setChanged(true);
				$this->setChanged(true);
			}
			
			// $this->{$name}->reloadControl($this, false, false, false);
			$this->selected_control_loaded = true;
		}
	}
	
	public function afterEvents()
	{
		$this->setViewstate("control_class", $this->select_control_class);
		$this->setViewstate("control_name", $this->select_control_name);
		$this->setViewstate("control_id", $this->select_control_id);
		
		// $this->debug("control_class: ".$this->select_control_class);
	}
	
	public function ___changeSelectedControl(QWebEvent $event, $has_changed = false)
	{
		// we do nothing at the moment we already used the event
	}
	
	/**
	 * Sets the selected control and returns the added control or null if none was added
	 *
	 * @param string $class
	 * @param string $name
	 * @param string $id
	 * @param boolean $call_init
	 * @param boolean $trigger_events
	 * @param boolean $load_viewstate
	 * @param boolean $load_postback
	 * @param boolean $call_load
	 * @param boolean $call_after_events
	 * @param boolean $forced_if_same_control
	 * @return QWebControl
	 */
	public function changeSelectedControl($class, $name = null, $id = null, 
		$call_init = true, $trigger_events = false, $load_viewstate = true, $load_postback = true, 
		$call_load = true, $call_after_events = false, $forced_if_same_control = false)
	{
		// should only change if needed
		if (!$name) $name = $class;
		if (!$id) $id = $name;

		if ( (!$forced_if_same_control) &&
			($class == $this->select_control_class) &&
			($name == $this->select_control_name) && 
			($id == $this->select_control_id))
		{
			return ;
		}
		
		
		$this->select_control_name = $name;
		$this->select_control_class = $class;
		$this->select_control_id = $id;

		if ($this->select_control_name)
		{
			$name = $this->select_control_name;
			$class = $this->select_control_class;
			$id = $this->select_control_id;

			if (QObject::IsA($class, "QWebControl"))
				eval("\$this->{$name} = new {$class}(\$id);");
			else 
				throw new Exception("Invalid panel class: {$class}");

			$this->removeAllControls();
			$this->addControl($this->{$name});

			$this->select_control_has_changed = true;
			$this->selected_control_loaded = true;
			$this->setChanged(true);
			
			// reload control
			// $this->debug("reload control: ".$this->select_control_name);
			$this->doLoadControl($this->getWebPage(), $call_init, $trigger_events, 
				$load_viewstate, $load_postback, $call_load, $call_after_events);

			$this->setChanged(true);
			$this->selected_control_loaded = true;
			
			return $this->{$name};
		}
		else
			return null;
	}
	
	/**
	 * Gets the selected control or null if none is set
	 *
	 * @return QWebControl
	 */
	public function getSelectedControl()
	{
		if ($this->select_control_name && isset($this->{$this->select_control_name}))
			return $this->{$this->select_control_name};
		else 
			return null;
	}
	
	public function getSelectedControlName()
	{
		return $this->select_control_name;
	}
	
	public function setDefaultControl($name, $class = null, $id = null)
	{
		$this->default_control_name = $name;
		$this->default_control_class = $class;
		$this->default_control_id = $id;
	}
	
	/*
	public function reloadSelectedControl()
	{
		if (!$this->select_control_class)
		{
			if ($this->getViewstate("control_class"))
			{
				$this->select_control_class = $this->getViewstate("control_class");
				$this->select_control_name  = $this->getViewstate("control_name");
				$this->select_control_id    = $this->getViewstate("control_id");
			}
			else if ($this->default_control_class)
			{
				$this->select_control_class = $this->default_control_class;
				$this->select_control_name  = $this->default_control_name;
				$this->select_control_id    = $this->default_control_id;
			}
		}
		else 
		{
			// nothing
		}
		
		
		$class = $this->select_control_class;
		$name = $this->select_control_name;
		$id = $this->select_control_id;

		if (QObject::IsA($class, "QWebControl"))
			eval("\$this->{$name} = new {$class}(\$id);");
		else 
			throw new Exception("Invalid panel class: {$class}");

		$this->removeAllControls();
		
		$vs_class = $this->getViewstate("control_class");
		$vs_name = $this->getViewstate("control_name");
		
		if ((!$this->select_control_has_changed) && $vs_class && ($vs_class == $class) && ($vs_name == $name))
		{
			// no change
		}
		else 
		{
			$this->select_control_has_changed = true;
			$this->{$name}->setChanged(true);
		}
		
		$this->{$name}->reloadControl($this, true, true, false);
		
		$this->setViewstate("control_class", $class);
		$this->setViewstate("control_name", $name);
		$this->setViewstate("control_id", $id);
	}
	*/
	
	/**
	 * Sets the selected control
	 *
	 * @param string $name 
	 * @param string $class
	 * @param string $id
	 */
	
	/*
	public function setSelectedControl($name, $class = null, $id = null, $set_changed = true)
	{
		if (!$id) $id = $name;
		if (!$class) $class = $name;
		
		$this->select_control_class = $class;
		$this->select_control_name = $name;
		$this->select_control_id = $id;
	}
	*/
}

?>